package com.shihan.controller;

import com.shihan.mapper.ArticleMapper;
import com.shihan.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ArticleHandler {

    @Autowired
    private ArticleMapper articleMapper;

    @RequestMapping("/toIndex")
    public String test(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize,
                       ModelMap map){

        // 简单处理一下不合法的数据；
        if(pageNo<=0) pageNo = 1;
        if(pageSize<=0) pageSize = 2;

        Integer pageStart = (pageNo-1)*pageSize;

        // 得到当前分页条件下的 list 集合数据；
        List<Article> pageList = articleMapper.getPageList(pageStart, pageSize);

        // 得到当前分页条件下，总的页码数；得到总的数据条数 / pageSize 并进行相关处理；
        Integer totalCounts = articleMapper.getTotalCounts();

        Integer totalPages = totalCounts / pageSize;
        if(totalCounts%pageSize!=0){
            totalPages++;
        }

        map.put("pageNo",pageNo);
        map.put("totalPages",totalPages);
        map.put("articleList",pageList);

        return "client/index";
    }

}
